﻿
using UnityEngine;
using UnityEngine.UI;

namespace XFGameFramework.TweenSystem
{

    /// <summary>
    /// ScrollRect动画拓展
    /// </summary>
    public static class ScrollRectExtensions 
    {

        /// <summary>
        /// 渐变HorizontalNormalizedPosition
        /// </summary>
        /// <param name="rect">ScrollRect对象</param>
        /// <param name="target">目标值</param>
        /// <param name="duration">持续时间</param>
        /// <param name="loop">循环模式</param>
        /// <param name="ease">缓动曲线</param>
        /// <param name="curve">自定义的缓动曲线</param>
        /// <param name="ingoreTimeScale">是否忽略时间缩放</param>
        /// <param name="delay">延迟</param>
        /// <param name="loopCount">循环次数</param>
        /// <returns></returns>
        public static TweenerCore<float> TweenHorizontalNormalizedPosition(this ScrollRect rect, float target, float duration, LoopMode loop = LoopMode.Once, Ease ease = Ease.Linear, AnimationCurve curve = null, bool ingoreTimeScale = false, float delay = 0, int loopCount = -1)
        {  
            return TweenManager.ComponentTween(rect, rect.horizontalNormalizedPosition, target, duration, loop, ease, curve, ingoreTimeScale, OnTweenHorizontalNormalizedPosition, delay,0,loopCount);
        }

        internal static void OnTweenHorizontalNormalizedPosition(TweenerCore<float> tweener)
        {
            if (tweener.Component is ScrollRect)
            {
                ScrollRect scrollRect = tweener.Component as ScrollRect;
                scrollRect.horizontalNormalizedPosition = tweener.Value;
            }
        }

        /// <summary>
        /// 渐变VerticalNormalizedPosition
        /// </summary>
        /// <param name="rect">ScrollRect对象</param>
        /// <param name="target">目标值</param>
        /// <param name="duration">持续时间</param>
        /// <param name="loop">循环模式</param>
        /// <param name="ease">缓动曲线</param>
        /// <param name="curve">自定义的缓动曲线</param>
        /// <param name="ingoreTimeScale">是否忽略时间缩放</param>
        /// <param name="delay">延迟</param>
        /// <param name="loopCount">循环次数</param>
        /// <returns></returns>
        public static TweenerCore<float> TweenVerticalNormalizedPosition(this ScrollRect rect, float target, float duration, LoopMode loop = LoopMode.Once, Ease ease = Ease.Linear, AnimationCurve curve = null, bool ingoreTimeScale = false, float delay = 0, int loopCount = -1)
        {
            return TweenManager.ComponentTween(rect, rect.verticalNormalizedPosition, target, duration, loop, ease, curve, ingoreTimeScale, OnTweenVerticalNormalizedPosition, delay,0,loopCount);
        }

        internal static void OnTweenVerticalNormalizedPosition(TweenerCore<float> tweener)
        {
            if (tweener.Component is ScrollRect)
            {
                ScrollRect scrollRect = tweener.Component as ScrollRect;
                scrollRect.verticalNormalizedPosition = tweener.Value;
            }
        }

        /// <summary>
        /// 渐变NormalizedPosition
        /// </summary>
        /// <param name="rect">ScrollRect对象</param>
        /// <param name="target">目标值</param>
        /// <param name="duration">持续时间</param>
        /// <param name="loop">循环模式</param>
        /// <param name="ease">缓动曲线</param>
        /// <param name="curve">自定义的缓动曲线</param>
        /// <param name="ingoreTimeScale">是否忽略时间缩放</param>
        /// <param name="delay">延迟</param>
        /// <param name="loopCount">循环次数</param>
        /// <returns></returns>
        public static TweenerCore<Vector2> TweenNormalizedPosition(this ScrollRect rect, Vector2 target, float duration, LoopMode loop = LoopMode.Once, Ease ease = Ease.Linear, AnimationCurve curve = null, bool ingoreTimeScale = false, float delay = 0, int loopCount = -1)
        { 
            return TweenManager.ComponentTween(rect, rect.normalizedPosition, target, duration, loop, ease, curve, ingoreTimeScale, OnTweenNormalizedPosition, delay,0,loopCount);
        }

        internal static void OnTweenNormalizedPosition(TweenerCore<Vector2> tweener)
        {
            if (tweener.Component is ScrollRect)
            {
                ScrollRect scrollRect = tweener.Component as ScrollRect;
                scrollRect.normalizedPosition = tweener.Value;
            }
        }

    }

}

